// miniprogram/pages/NX_detailProject/detailProject.js
import configGantt from '../../library/configGantt'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        onShow:false,

        windowWidth: getApp().globalData.windowWidthRpx,
        windowHeight: getApp().globalData.windowHeightRpx,

        projectId: null,
        master: false,
        self: null,
        message: '',
        maskShow: true,

        group: null,
        member: [],
        mission: [],
        gantt: null,

        addButtonStyle: '',

        infoStep: 0,
        infoCoverStyle: '',
        infoCoverMinTop: 90,
        infoCoverDeltaY: 0,
        infoCoverMaxTop: getApp().globalData.windowHeight - 120 * getApp().globalData.scaleRate,
        infoCoverLastPos: 0,
        infoCoverMoveSpeed: 0,
        infoMoveLock: null,
        coverOpacity: 1,
        exitConfirm: false,
        exitText: '退出项目',
        doExit:false,

        infoBalloon: {
            display: false,
            x: 0,
            y: 0
        },
        infoItem: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    init() {
        wx.cloud.callFunction({
            name: 'getGroupInfo',
            data: {
                group: this.data.projectId,
                needDetail: true
            }
        }).then(detail => {
            if(!this.data.onShow) return;
            wx.setNavigationBarTitle({
                title: detail.result.group.project.projectName,
            })
            configGantt(detail.result.mission, 100, {
                start: detail.result.group.project.startDate,
                end: detail.result.group.project.endDate,
            })
                .then(gantt => {
                    this.setData({
                        maskShow: false,
                        group: detail.result.group,
                        member: detail.result.member,
                        mission: detail.result.mission,
                        gantt
                    })
                })

        })
    },
    onLoad: function (options) {
        this.setData({
            projectId: options.projectId,
            master: options.master === 'true',
            self: getApp().globalData.userInfo,
            exitConfirm: false,
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //拉取项目详情
        this.init();
        this.data.onShow=true;
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () { 
        this.data.onShow=false;
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },
    setInfoUp() {
        this.setData({
            infoCoverStyle: `top:${this.data.infoCoverMinTop}px;transition:top 0.5s ease`,
            infoStep: 1,
            coverOpacity: 1
        })
    },
    setInfoDown() {
        this.setData({
            infoCoverStyle: `top:${this.data.infoCoverMaxTop}px;transition:top 0.5s ease`,
            infoStep: 0,
            coverOpacity: 0,
        })
    },
    infostart(e) {
        // if (this.data.infoStep === 1) return;
        this.data.infoCoverDeltaY = e.touches[0].clientY - e.currentTarget.offsetTop;
        this.data.infoCoverLastPos = {
            top: e.currentTarget.offsetTop,
            time: e.timeStamp
        }
    },
    infomove(e) {
        // if (this.data.infoStep === 1) return;
        if (this.data.infoMoveLock) return;
        this.data.infoCoverMoveSpeed = (e.currentTarget.offsetTop - this.data.infoCoverLastPos.top) / (e.timeStamp - this.data.infoCoverLastPos.time);
        this.data.infoCoverLastPos = {
            top: e.currentTarget.offsetTop,
            time: e.timeStamp
        }
        // console.log(this.data.infoCoverMoveSpeed)
        let top = e.touches[0].clientY - this.data.infoCoverDeltaY;
        if (top < this.data.infoCoverMinTop) top = this.data.infoCoverMinTop;
        else if (top > this.data.infoCoverMaxTop) top = this.data.infoCoverMaxTop;
        this.setData({
            infoCoverStyle: `top:${top}px`,
            infoStep: -1,
            coverOpacity: 1 - (top - this.data.infoCoverMinTop) / (this.data.infoCoverMaxTop - this.data.infoCoverMinTop)
        })
    },
    infomoveend(e) {
        if (this.data.infoMoveLock) return;
        if (Math.abs(this.data.infoCoverMoveSpeed) > 0.3) {
            if (this.data.infoCoverMoveSpeed < 0) {
                this.setInfoUp()
            } else {
                this.setInfoDown()
            }
        } else if (e.currentTarget.offsetTop < (this.data.infoCoverMaxTop + this.data.infoCoverMinTop) / 2) {
            this.setInfoUp();
        } else {
            this.setInfoDown();
        }
    },
    infotouch() {
        if (this.data.infoStep === 0) {
            this.setInfoUp();
            this.data.infoMoveLock = setTimeout(() => {
                this.data.infoMoveLock = null
            }, 500)
        }

    },
    fold() {
        this.setInfoDown()
        this.data.infoMoveLock = setTimeout(() => {
            this.data.infoMoveLock = null
        }, 500)
    },
    infotouchlong() {
        if (this.data.infoStep === 1) {
            this.fold();
        }
    },
    movedoing(e) {
        this.setData({
            addButtonStyle: `left:${e.touches[0].clientX}px;top:${e.touches[0].clientY}px;transform:translate(-50%,-50%);`
        })
    },
    moveend(e) {
        let top = e.currentTarget.offsetTop;
        if (top < 60) top = 60;
        else if (top > getApp().globalData.windowHeight - 80) top = getApp().globalData.windowHeight - 80
        const left = getApp().globalData.windowWidth / 2 > e.currentTarget.offsetLeft;
        this.setData({
            addButtonStyle: `top:${top}px;left:${left ? 20 : getApp().globalData.windowWidthRpx - 120}rpx;transition:all 0.5s ease;`
        })
    },
    newMission() {
        getApp().globalData.tempProject = {
            group: this.data.group,
            mission: this.data.gantt.users,
            member: this.data.member,
        }
        wx.navigateTo({
            url: '../NX_newMission/newMission?create=true&master=' + this.data.master + '&projectId=' + this.data.projectId,
        })
    },
    tryExit() {
        if (!this.data.exitConfirm) {
            this.setData({
                exitConfirm: setTimeout(() => {
                    this.setData({
                        exitConfirm: null
                    })
                }, 5000)
            })
        } else {
            this.setData({
                exitConfirm: null,
                // exitText: '请稍后'
                doExit:true
            })
            wx.cloud.callFunction({
                name: 'delCode',
                data: {
                    userId: this.data.self._id,
                    groupId: this.data.projectId
                }
            }).then(() => {
                this.setData({
                    doExit:false
                })
                wx.navigateBack({
                    delta: 1,
                })
            }).catch(err => {
                this.setData({
                    doExit:false
                })
                console.log(err)
            })

        }
    },
    setProject() {
        wx.navigateTo({
            url: `../NX_setProject/setProject?projectId=${this.data.projectId}&projectName=${this.data.group.project.projectName}&startDate=${this.data.group.project.startDate}&endDate=${this.data.group.project.endDate}`
        })
    },
    manageMember(){
        wx.navigateTo({
            url:`../NX_detailProjectMember/detailProjectMember?projectId=${this.data.projectId}`
        })
    },
    onItemClick(e) {
        // console.log(e)
        this.setData({ infoItem: e.detail.item });
        this.disableInfoBalloon();
        this.enableInfoBalloon(e.detail.x, e.detail.y)
    },
    onItemDoubleClick(e){
        getApp().globalData.tempMissionDetail=e.detail.item;
        wx.navigateTo({
            url:`../NX_detailMission/detailMission?missionId=${e.detail.item._id}`
        })
    },
    enableInfoBalloon(tx, ty) {
        const {
            windowWidth,
            scaleRate,
            windowHeight
        } = getApp().globalData;
        const minX = 400 / 2 * scaleRate;
        const maxX = windowWidth - minX;
        const minY = 120 * scaleRate;
        const maxY = windowHeight - minY;
        // console.log(minX,maxX,minY,maxY)
        let x = tx, y = ty;
        if (x < minX) x = minX;
        else if (x > maxX) x = maxX;
        if (y < minY) y = minY;
        else if (y > maxY) y = maxY;
        this.setData({
            infoBalloon: {
                display: true,
                x,
                y
            }
        })
    },
    disableInfoBalloon() {
        this.setData({
            infoBalloon: {
                display: false
            }
        })
    },
    onShareAppMessage(e){
        return{
            title:`邀请加入项目${this.data.group.project.projectName}`,
            path:`/pages/NX_index/index?invite=true&inviter=${this.data.self._id}&projectId=${this.data.projectId}&time=${new Date().getTime()}`,
            imageUrl:'../../images/nexeam_banner.png'
        }
    },
    breakGroup(){
        wx.showModal({
            title:'提示',
            content:'此操作将清除所有成员以及项目中的任务，您确定解散吗',
            success:(res)=>{
                if(res.confirm){
                    wx.showModal({
                        title:'警告',
                        content:'解散操作不可逆，您了解了吗',
                        confirmText:'解散',
                        confirmColor:'#ff0000',
                        success:res=>{
                            if(res.confirm){
                                wx.cloud.callFunction({
                                    name:'breakGroup',
                                    data:{
                                        projectId:this.data.group._id
                                    }
                                }).then(()=>{
                                    wx.navigateBack({
                                        delta:1
                                    })
                                })
                            }
                        }
                    })
                }
            }
        })
    }
})